package com.caipiao.lottery.task.keepline;

import com.caipiao.lottery.log.TraceLogger;
import jodd.props.Props;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Properties;


public class KeepLineManager
{
    private static KeepLineManager instance = new KeepLineManager();
    boolean init_flag =false;

    private ArrayList<KeepLineThread> query_thrd_list = new ArrayList<KeepLineThread>();

    private KeepLineManager(){}

    public static KeepLineManager Instance()
    {
        return instance;
    }

    public void InitTask(Props props)
    {
        if (this.init_flag)
        {
            return;
        }

        this.init_flag = true;

        //启动各个分发线程
        String taskList = props.getValue("KeepLineTask");
        String[] task_list = taskList.split(",");

        for (String Name : task_list)
        {
            Properties prop = new Properties();
            props.extractProps(prop, Name);

            if (!prop.getProperty("KeepLine.Valid").equalsIgnoreCase("1"))
            {
                continue;
            }

            String LoggerName =  prop.getProperty("KeepLine.Logger");

            int ReadInterval = Integer.valueOf(prop.getProperty("KeepLine.ReadInterval"));
            int ReadCount = Integer.valueOf(prop.getProperty("KeepLine.ReadCount"));
            int ProcInterval = Integer.valueOf(prop.getProperty("KeepLine.ProcInterval"));

            Logger logger = LoggerFactory.getLogger(LoggerName);

            KeepLineThread query_thrd = new KeepLineThread(Name, ReadInterval, ReadCount, ProcInterval, logger);
            query_thrd.start();
            this.query_thrd_list.add(query_thrd);

            TraceLogger.System.debug(Name + " KeepLineThread 保持在线，线程启动成功");
        }

    }
}

